package com.yzcity.cinema.web.rest;

import com.yzcity.cinema.domain.BaseTypeValue;
import com.yzcity.cinema.repository.BaseTypeValueRepository;
import com.yzcity.cinema.web.rest.errors.BadRequestAlertException;

import io.github.jhipster.web.util.HeaderUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing {@link com.yzcity.cinema.domain.BaseTypeValue}.
 */
@RestController
@RequestMapping("/api")
@Transactional
public class BaseTypeValueResource {

    private final Logger log = LoggerFactory.getLogger(BaseTypeValueResource.class);

    private static final String ENTITY_NAME = "baseTypeValue";

    @Value("${jhipster.clientApp.name}")
    private String applicationName;

    private final BaseTypeValueRepository baseTypeValueRepository;

    public BaseTypeValueResource(BaseTypeValueRepository baseTypeValueRepository) {
        this.baseTypeValueRepository = baseTypeValueRepository;
    }

    /**
     * {@code POST  /base-type-values} : Create a new baseTypeValue.
     *
     * @param baseTypeValue the baseTypeValue to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new baseTypeValue, or with status {@code 400 (Bad Request)} if the baseTypeValue has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/base-type-values")
    public ResponseEntity<BaseTypeValue> createBaseTypeValue(@Valid @RequestBody BaseTypeValue baseTypeValue) throws URISyntaxException {
        log.debug("REST request to save BaseTypeValue : {}", baseTypeValue);
        if (baseTypeValue.getId() != null) {
            throw new BadRequestAlertException("A new baseTypeValue cannot already have an ID", ENTITY_NAME, "idexists");
        }
        BaseTypeValue result = baseTypeValueRepository.save(baseTypeValue);
        return ResponseEntity.created(new URI("/api/base-type-values/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(applicationName, false, ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * {@code PUT  /base-type-values} : Updates an existing baseTypeValue.
     *
     * @param baseTypeValue the baseTypeValue to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated baseTypeValue,
     * or with status {@code 400 (Bad Request)} if the baseTypeValue is not valid,
     * or with status {@code 500 (Internal Server Error)} if the baseTypeValue couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/base-type-values")
    public ResponseEntity<BaseTypeValue> updateBaseTypeValue(@Valid @RequestBody BaseTypeValue baseTypeValue) throws URISyntaxException {
        log.debug("REST request to update BaseTypeValue : {}", baseTypeValue);
        if (baseTypeValue.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        BaseTypeValue result = baseTypeValueRepository.save(baseTypeValue);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(applicationName, false, ENTITY_NAME, baseTypeValue.getId().toString()))
            .body(result);
    }

    /**
     * {@code GET  /base-type-values} : get all the baseTypeValues.
     *
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of baseTypeValues in body.
     */
    @GetMapping("/base-type-values")
    public List<BaseTypeValue> getAllBaseTypeValues() {
        log.debug("REST request to get all BaseTypeValues");
        return baseTypeValueRepository.findAll();
    }

    /**
     * {@code GET  /base-type-values/:id} : get the "id" baseTypeValue.
     *
     * @param id the id of the baseTypeValue to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the baseTypeValue, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/base-type-values/{id}")
    public ResponseEntity<BaseTypeValue> getBaseTypeValue(@PathVariable Long id) {
        log.debug("REST request to get BaseTypeValue : {}", id);
        Optional<BaseTypeValue> baseTypeValue = baseTypeValueRepository.findById(id);
        return ResponseUtil.wrapOrNotFound(baseTypeValue);
    }

    /**
     * {@code DELETE  /base-type-values/:id} : delete the "id" baseTypeValue.
     *
     * @param id the id of the baseTypeValue to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @DeleteMapping("/base-type-values/{id}")
    public ResponseEntity<Void> deleteBaseTypeValue(@PathVariable Long id) {
        log.debug("REST request to delete BaseTypeValue : {}", id);
        baseTypeValueRepository.deleteById(id);
        return ResponseEntity.noContent().headers(HeaderUtil.createEntityDeletionAlert(applicationName, false, ENTITY_NAME, id.toString())).build();
    }
}
